<?php

namespace app\admin\controller\sum;

use app\admin\model\sum\Config as SumConfig;
use app\admin\model\User;

class Config extends Common
{
    protected $noNeedRight = ['index', 'platformStatus', 'getPlatformUrl'];

    public function index()
    {
        $configList = [
            [
                'label' => '基本信息',
                'name' => 'sum/config/basic',
                'status' => $this->auth->check('sum/config/basic')
            ],
            [
                'label' => '用户配置',
                'name' => 'sum/config/user',
                'status' => $this->auth->check('sum/config/user')
            ],
//            [
//                'label' => '平台配置',
//                'name' => 'sum/config/platform',
//                'status' => $this->auth->check('sum/config/platform')
//            ],
//            [
//                'label' => '语言配置',
//                'name' => 'sum/config/language',
//                'status' => $this->auth->check('sum/config/language')
//            ],
            [
                'label' => 'Redis配置',
                'name' => 'sum/config/redis',
                'status' => $this->auth->check('sum/config/redis')
            ]
        ];
        $this->assignconfig("configList", $configList);
        return $this->view->fetch();
    }

    /**
     * 基本配置
     */
    public function basic()
    {
        if ('GET' === $this->request->method()) {

            $configs = SumConfig::getConfigs('shop.basic', false);
        } elseif ('POST' === $this->request->method()) {

            $configs = SumConfig::setConfigs('shop.basic', $this->request->param());
        }
        $this->success('操作成功', null, $configs);
    }

    /**
     * 语言配置
     */
    public function language()
    {
        if ('GET' === $this->request->method()) {

            $configs = SumConfig::getConfigs('language', false);
        } elseif ('POST' === $this->request->method()) {

            $configs = SumConfig::setConfigs('language', $this->request->param());
        }
        $this->success('操作成功', null, $configs);
    }


    /**
     * 用户默认配置
     */
    public function user()
    {
        if ('GET' === $this->request->method()) {

            $configs = SumConfig::getConfigs('shop.user', false);
        } elseif ('POST' === $this->request->method()) {

            $configs = SumConfig::setConfigs('shop.user', $this->request->param());
        }
        $this->success('操作成功', null, $configs);
    }

    /**
     * 平台状态
     */
    public function platformStatus()
    {
        $status = [
            'H5' => SumConfig::getConfigs('shop.platform.H5.status', false),
            'App' => SumConfig::getConfigs('shop.platform.App.status', false),
            'WechatMiniProgram' => SumConfig::getConfigs('shop.platform.WechatMiniProgram.status', false),
            'WechatOfficialAccount' => SumConfig::getConfigs('shop.platform.WechatOfficialAccount.status', false),
        ];

        $this->success('操作成功', null, $status);
    }

    /**
     * 平台配置
     */
    public function platform($platform)
    {
        if (!$this->request->isAjax()) {
            return $this->view->fetch();
        }

        if (!in_array($platform, ['App', 'H5', 'WechatMiniProgram', 'WechatOfficialAccount'])) {
            $this->error('平台不支持');
        }

        if ('GET' === $this->request->method()) {

            $configs = SumConfig::getConfigs('shop.platform.' . $platform, false);
        } elseif ('POST' === $this->request->method()) {
            $params = $this->request->param();
            if (!isset($params['share']['methods'])) {
                $params['share']['methods'] = [];
            }
            if (!isset($params['payment']['methods'])) {
                $params['payment']['methods'] = [];
            }
            $configs = SumConfig::setConfigs('shop.platform.' . $platform, $params);
        }
        $this->success('操作成功', null, $configs);
    }

    /**
     * redis 配置
     */
    public function redis()
    {
        if ('GET' === $this->request->method()) {
            $default = [
                'host' => '127.0.0.1',              // redis 主机地址
                'password' => '',                   // redis 密码
                'port' => 6379,                     // redis 端口
                'select' => 1,                      // redis 数据库
                'timeout' => 0,                     // redis 超时时间
                'persistent' => false,              // redis 持续性，连接复用
            ];
            $redis = \think\Config::get('redis');
            $redis['empty_password'] = 0;
            $redis['password'] = '';                // 隐藏密码
            $configs = $redis ? array_merge($default, $redis) : $default;
        } elseif ('POST' === $this->request->method()) {
            operate_filter();
            $configs = $this->request->param();
            $empty_password = (int)$configs['empty_password'];      // 是否设置空密码
            unset($configs['empty_password']);

            if (isset($configs['password']) && empty($configs['password'])) {
                $redis = \think\Config::get('redis');
                // 不修改密码，保持为原始值
                $configs['password'] = $redis['password'] ?? '';
            } elseif ($empty_password) {
                $configs['password'] = '';
            }

            $configs['persistent'] = (isset($configs['persistent']) && ($configs['persistent'] === true || $configs['persistent'] == 'true')) ? true : false;

            // 存文件
            file_put_contents(
                ROOT_PATH . 'application' . DS . 'extra' . DS . 'redis.php',
                '<?php' . "\n\nreturn " . short_var_export($configs, true) . ";"
            );
        }
        $this->success('操作成功', null, $configs);
    }

    public function getPlatformUrl()
    {
        $h5Url = SumConfig::getConfigField('shop.basic.domain');
        $wechatMpAppid =  SumConfig::getConfigField('shop.platform.WechatMiniProgram.app_id');

        $this->success('', null, [
            'url' => $h5Url,
            'appid' => $wechatMpAppid
        ]);
    }
}
